#include "register.h"
#include "ui_register.h"

#include "library.h"
#include "mylibrary.h"

#include <QMessageBox>
#include <QSqlTableModel>
#include <QSqlDatabase>
#include <QSqlError>
#include <QDebug>
#include <QSqlQuery>

Register::Register(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
}

Register::~Register()
{
    delete ui;
}

void Register::on_pushButton_2_clicked()
{
    close();
}

void Register::on_pushButton_clicked()
{
    if(ui->lineEditAccount->text().isEmpty() ||
            ui->lineEditEmail->text().isEmpty() ||
            ui->lineEditPassword->text().isEmpty() ||
            ui->lineEditPasswordConfirm->text().isEmpty() ||
            ui->lineEditPhone->text().isEmpty() ||
            !(ui->radioButtonMan->isChecked() || ui->radioButtonWoman->isChecked()))
    {
        QMessageBox:: critical(this, "Error", "Please input completely!");
    }
    else
    {
        if(ui->lineEditPassword->text() == ui->lineEditPasswordConfirm->text())
        {
            QSqlQuery query;
            query.prepare("INSERT INTO user(Account, Password, Sex, Phone, Email, Authorization)"
                          "VALUES(:Account, :Password, :Sex, :Phone, :Email, :Authorization)");
            query.bindValue(":Account", ui->lineEditAccount->text());
            query.bindValue(":Password", ui->lineEditPassword->text());
            query.bindValue(":Sex", NULL);
            query.bindValue(":Phone", ui->lineEditPhone->text());
            query.bindValue(":Email", ui->lineEditEmail->text());
            query.bindValue(":Authorization", 0);
            bool ok = query.exec();
            if(!ok)
            {
                QMessageBox:: critical(this, "Error", "Insert Error");
            }
            else
            {
                qDebug() << "数据插入成功";
                Library *b_s = new Library();
                b_s->show();
                close();
            }
        }
        else
        {
            QMessageBox::critical(this,"Error", "前后密码不一致");
        }
    }
}

void Register::on_pushButtonReturnMylibrar_clicked()
{
    myLibrary *m_b = new myLibrary();
    m_b->show();
    close();
}
